/*
 * imgup - v1.0.0
 * Easy imgupload jQuery plugin, modified from ueditor plugin
 *
 * Made by wangchao
 */
;(function($) {
	var pluginName = "imgup", defaults = {
		muti : false,
		imgs : []
	};
	var setImgs = function($this,muti){
		if(muti){
			var imgArr = new Array();
			$this.prev('ul').find('img').each(function(){
				imgArr.push($(this).attr('src'));
			});
			$this.val(imgArr.toString());
		}
		else{
			$this.val($this.prev('ul').find('img').eq(0).attr('src'));
		}
	};
	var bindEvents = function($this,muti){
		$this.prev('ul').find('.img-list-btndel').off('click');
		$this.prev('ul').find('.img-list-btndel').on('click',function(){
			var start = $(this).parent('li').parent('ul').children('li').index($(this).parent('li'));
			//删除数组指定位置元素
			$(this).parent('li').remove();
			setImgs($this,muti);
		});
		if(muti){
			$this.prev('ul').find('.img-move-right').off('click');
			$this.prev('ul').find('.img-move-right').on('click',function(){
				var $list = $(this).parent('li').parent('ul').children('li');
				var index = $list.index($(this).parent('li'));
				//位置下移
				if($list.length - index > 2){
					$this.prev('ul').children('li').eq(index+1).after($this.prev('ul').children('li').eq(index));
					setImgs($this,muti);
				}
			});
			$this.prev('ul').find('.img-move-left').off('click');
			$this.prev('ul').find('.img-move-left').on('click',function(){
				var $list = $(this).parent('li').parent('ul').children('li');
				var index = $list.index($(this).parent('li'));
				//位置上移
				if(index > 0){
					$this.prev('ul').children('li').eq(index-1).before($this.prev('ul').children('li').eq(index));
					setImgs($this,muti);
				}					
			});
		}
		$this.prev('ul').find('.img-list-add').off('click');
		$this.prev('ul').find('.img-list-add').on('click',function(){
			layer.open({type: 2,title:'图片上传',area: ['650px', '490px'],fix: false,maxmin: false,content:"/assets/ueditor/dialogs/imagebtn/image.html",end:function(){
				if(layer.imgList&&layer.imgList.length>0){
					var $list = $this.prev('ul').children('li');
					var newImgs = layer.imgList;
					if(muti){
						for(var i=0;i<newImgs.length;i++){
							var newImg = '<li><span class="img-move img-move-left"></span><span class="img-move img-move-right"></span><span class="img-list-btndel j-delimg"><i class="fa fa-trash-o"></i></span><span class="img-list-overlay"></span><img src="'+newImgs[i].src+'"></li>';
							$list.eq($list.length-1).before(newImg);
						}
					}
					else{
						var newImg = '<li><span class="img-move img-move-left"></span><span class="img-move img-move-right"></span><span class="img-list-btndel j-delimg"><i class="fa fa-trash-o"></i></span><span class="img-list-overlay"></span><img src="'+newImgs[0].src+'"></li>';
						$list.eq($list.length-1).before(newImg);
						if($list.length>1){
							$list.eq(0).remove();
						}
					}
					bindEvents($this,muti);
					setImgs($this,muti);
				}
			}});
		});
	};
	
	var methods = {
		init : function(options) {
			return this.each(function() {
				var $this = $(this);
				var settings = $.extend({}, defaults, options);
				var imglist = '<ul class="img-list clearfix">';
				var currImgs = settings.imgs;
				if($this.val()!=''){
					currImgs = $this.val().split(',');
				}
				$.each(currImgs, function(i, n){
					if(settings.muti||i==0){	
						imglist += '<li><span class="img-move img-move-left"></span><span class="img-move img-move-right"></span><span class="img-list-btndel j-delimg"><i class="fa fa-trash-o"></i></span><span class="img-list-overlay"></span><img src="'+n+'"></li>';
					}
				});
				imglist += '<li class="img-list-add j-addimg">+</li></ul>';
				$this.before(imglist);
				bindEvents($this,settings.muti);
			});
		},
		getImgs : function() {
			var $this = $(this);
			if(!$this.is(':input')){
	            $.error(pluginName +' need input');
	            return;
			}
			return $this.val();
		},
	};

	$.fn[pluginName] = function() {
        var method = arguments[0]; 
        if(methods[method]) {
            method = methods[method];
            arguments = Array.prototype.slice.call(arguments, 1);
        } else if( typeof(method) == 'object' || !method ) {
            method = methods.init;
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.imgup' );
            return this;
        }
        return method.apply(this, arguments); 
    };
    
})(jQuery);